package VO

import "eag_arch_layout/internal/model"

// PageSearchModel 分页的请求
type PageSearchModel struct {
	Limit       int `json:"limit"`
	Offset      int `json:"offset"`
	PageIndex   int `json:"pageIndex"`
	Sort        string
	Order       string
	searchParam map[string]interface{}
}

func (m *PageSearchModel) GetSearchParam() map[string]interface{} {
	return m.searchParam
}

// NewPageSearchModel 构造函数
func NewPageSearchModel(para map[string]interface{}) *PageSearchModel {
	searchModel := &PageSearchModel{}
	if v, ok := para["size"]; ok {
		searchModel.Limit = int(v.(float64))
		delete(para, "size")
	} else {
		searchModel.Limit = model.DEFAULT_PAGE_SIZE
	}
	if v, ok := para["page"]; ok {
		searchModel.PageIndex = int(v.(float64))
		delete(para, "page")
	} else {
		searchModel.PageIndex = 1
	}
	searchModel.Offset = (searchModel.PageIndex - 1) * searchModel.Limit
	if v, ok := para["sort"]; ok {
		searchModel.Sort = v.(string)
		delete(para, "sort")
	}
	if v, ok := para["order"]; ok {
		searchModel.Order = v.(string)
		delete(para, "order")
	}
	searchModel.searchParam = para
	return searchModel
}
